package com.yc.baihe.biz.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yc.baihe.beans.UserList;
import com.yc.baihe.biz.IUserListBiz;
import com.yc.baihe.dao.DBHelper;

public class UserListBizImpl implements IUserListBiz {
	DBHelper db=new DBHelper();
	
	public List<UserList> find(Integer pageNo, Integer pageSize) {
		String sql;
		List<Object> params=new ArrayList<Object>();
		if(pageNo==0){
			sql="select * from userlist where txStatus=0";
		}else{
			sql="select * from(select a.*,rownum rn from (select * from UserList order by buid) a where rownum<=?) b where rn>? and txStatus=0";
			params.add(pageNo*pageSize);
			params.add((pageNo-1)*pageSize);
		}
		System.out.println(db.find(sql,params,UserList.class));
		return db.find(sql,params,UserList.class);
	}
	
	public List<UserList> RanfindMember(int pageNo,String sex) {
		List<Object> params=new ArrayList<Object>();
		String sql;
		if(pageNo==0){
			sql="select * from UserList where txStatus=1 and sex=?";
			params.add(sex);
		}else{
			sql="select * from(select a.*,rownum rn from (select * from UserList where txStatus=1 and sex=?" +
			 "order by buid) a where rownum<=?) b where rn>? ";
			params.add(sex);
			params.add(pageNo+20);
			params.add(pageNo);
		}
		
		return db.find(sql, params,UserList.class);
	}
	
	public int getTotal() {
		String sql="select count(buid) from UserList";
		List<List<String>> list=db.finds(sql, null);
		if(list!=null && list.size()>0){
			return Integer.parseInt(list.get(0).get(0));
		}
		return 0;
	}


	public String findNicknamefromBuid(String buid) {
		String sql="select nickname from userlist where buid=?";
		List<Object> params=new ArrayList<Object>();
		params.add(buid);
		List<List<String>> list=db.finds(sql, params);
		if(list.size()==0){
			return null;
		}else{
			return db.finds(sql, params).get(0).get(0);
		}
	}


	public List<Map<String,Object>> findbByBuid(int buid) {
		String sql="select * from userlist where buid=?";
		List<Object> params=new ArrayList<Object>();
		params.add(buid);
		
		return db.find(sql, params);
	}

	public int total_Sex(String sex) {
		String sql="select count(buid) from userlist where txStatus=1 and sex=?";
		List<Object> params=new ArrayList<Object>();
		params.add(sex);
		List<List<String>> list=db.finds(sql, params);
		if(list!=null && list.size()>0){
			return Integer.parseInt(list.get(0).get(0));
		}
		return 0;
	}

	public List<UserList> doSearchMember(String xianarea, String looks,int pageNo) {
		String sql;
		List<Object> params =new ArrayList<Object>();
		
		if(xianarea==null || "".equals(xianarea)){
			sql="select * from userlist where txStatus=1 and looks=?";
//			sql="select * from(select a.*,rownum rn from (select * from UserList where looks=?" +
//					"order by buid) a where rownum<=?) b where rn>? ";
			params.add(looks);
//			params.add(pageNo+20);
//			params.add(pageNo);
			
		}else{
			sql="select * from userlist where txStatus=1 and xianarea=? ";
			sql+=" looks=?";
			params.add(xianarea);
			params.add(looks);
		}
			
		return db.find(sql, params, UserList.class);
	}

	public int updateUserPicStatus(String buids, int status) {
		if(buids.indexOf("or")>0){
			return 0;
		}else{
			String sql="update userlist set txStatus="+status+" where buid in("+buids+")";
			return db.update(sql, null);
		}
	}

	

	public List<UserList> findbByBuid(String buid) {
		String sql="select * from userlist where buid=?";
		List<Object> params=new ArrayList<Object>();
		params.add(buid);
		
		System.out.println(db.find(sql,params,UserList.class));
		return db.find(sql, params,UserList.class);
	}


	public int UpdatePhoto(int buid, String touxiang) {
		 String sql="update userlist set touxiang=? where buid=?";
		 List<Object> params=new ArrayList<Object>();
		 params.add(touxiang);
		 params.add(buid);
		 return db.update(sql, params);
	}
	
	/***
	 * 添加会员信息  
	 */
	public int addUserList(UserList ul) {
		String sql="insert into userList values(?,?,?,?,0,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		List<Object> params=new ArrayList<Object>();
		params.add(ul.getNickname());
		params.add(ul.getBuid());
		params.add(ul.getTouxiang());
		params.add(ul.getUname());
		params.add(ul.getSex());
		params.add(ul.getBirthday());
		params.add(ul.getAnimal());
		params.add(ul.getConstellation());
		params.add(ul.getHometown());
		params.add(ul.getXianarea());
		params.add(ul.getHeight());
		params.add(ul.getEducation());
		params.add(ul.getWorks());
		params.add(ul.getIncome());
		params.add(ul.getMarriage());
		params.add(ul.getChildren());
		params.add(ul.getIfhome());
		params.add(ul.getifchar());
		params.add(ul.getLooks());
		params.add(ul.getContents());
		params.add(ul.getTemp1());
		params.add(ul.getTemp2());
			
		return db.update(sql, params);
		
	}

	public int update(UserList bu) {
		String sql="update userlist set buid=buid";
		List<Object> params=new ArrayList<Object>();
		if(bu.getNickname()!=null){
			sql+=",nickname=?";
			params.add(bu.getNickname());
		}
		if(bu.getTouxiang()!=null){
			sql+=",tongxiang=?";
			params.add(bu.getTouxiang());
		}
		if(bu.getUname()!=null){
			sql+=",uname=?";
			params.add(bu.getUname());
		}
		if(bu.getSex()!=null){
			sql+=",sex=?";
			params.add(bu.getSex());
		}
		if(bu.getBirthday()!=null){
			sql+=",birtday=?";
			params.add(bu.getBirthday());
		}
		if(bu.getAnimal()!=null){
			sql+=",animal=?";
			params.add(bu.getAnimal());
		}
		if(bu.getConstellation()!=null){
			sql+=",constellation=?";
			params.add(bu.getConstellation());
		}
		if(bu.getHometown()!=null){
			sql+=",hometown=?";
			params.add(bu.getHometown());
		}
		if(bu.getXianarea()!=null){
			sql+=",xianarea=?";
			params.add(bu.getXianarea());
		}
		if(bu.getHeight()!=null){
			sql+=",height=?";
			params.add(bu.getHeight());
		}
		if(bu.getEducation()!=null){
			sql+=",education=?";
			params.add(bu.getEducation());
		}
		if(bu.getWorks()!=null){
			sql+=",works=?";
			params.add(bu.getWorks());
		}
		if(bu.getIncome()!=null){
			sql+=",income=?";
			params.add(bu.getIncome());
		}
		if(bu.getMarriage()!=null){
			sql+=",marriage=?";
			params.add(bu.getMarriage());
		}
		if(bu.getChildren()!=null){
			sql+=",children=?";
			params.add(bu.getChildren());
		}
		if(bu.getIfhome()!=null){
			sql+=",ifhome=?";
			params.add(bu.getIfhome());
		}
		if(bu.getifchar()!=null){
			sql+=",ifchar=?";
			params.add(bu.getifchar());
		}
		if(bu.getLooks()!=null){
			sql+=",looks=?";
			params.add(bu.getLooks());
		}
		
		if(bu.getContents()!=null){
			sql+=",contents=?";
			params.add(bu.getContents());
		}
		if(bu.getTemp1()!=null){
			sql+=",temp1=?";
			params.add(bu.getTemp1());
		}
		if(bu.getTemp2()!=null){
			sql+=",temp2=?";
			params.add(bu.getTemp2());
		}
		sql+=" where buid=?";
		params.add(bu.getBuid());
		return db.update(sql, params);
	}

	public List<UserList> findbByBuids(int buid) {
		String sql="select * from userlist where buid=?";
		List<Object> params=new ArrayList<Object>();
		params.add(buid);
		return db.find(sql, params, UserList.class);
	}

	public List<Map<String, Object>> findLooks(String sex) {
		List<Object> params=new ArrayList<Object>();
		
		String sql="select distinct(looks) from userlist where ";
		if("".equals(sex) || sex==null){
			sql+="1=1";
			
			return db.find(sql,null);
		}else{
			sql+="sex=?";
			if("boy".equals(sex)){
				sex="男";
			}else{
				sex="女";
			}
			params.add(sex);
			return db.find(sql, params);
		}
		
		
	}

	public List<UserList> findBysal(String sex,int age, String looks) {
		String sql="select * from userlist where txStatus=0 and sex=? and looks=?";
		List<Object> params=new ArrayList<Object>();
		params.add(sex);
		params.add(looks);
		System.out.println(db.find(sql,params,UserList.class));
		return db.find(sql, params,UserList.class);
	}


}
